package com.baomidou.springwind.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.springwind.utils.SpringBeanUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.kisso.SSOAuthorization;
import com.baomidou.kisso.Token;
import com.baomidou.springwind.entity.Permission;
import com.baomidou.springwind.entity.vo.MenuVO;
import com.baomidou.springwind.mapper.PermissionMapper;
import com.baomidou.springwind.service.IPermissionService;
import com.baomidou.springwind.service.support.BaseServiceImpl;

/**
 * <p>
 * Permission 表数据服务层接口实现类
 * </p>
 * <p>
 * 实现权限接口 SSOAuthorization
 * </p>
 */
@Service
public class PermissionServiceImpl extends BaseServiceImpl<PermissionMapper, Permission>
        implements IPermissionService, SSOAuthorization {

    @Cacheable(value = "menuCache", key = "#userId")
    @Override
    public List<MenuVO> selectMenuVOByUserId(Long userId) {
        //查询该ID所有的所有菜单
        List<MenuVO> perList = baseMapper.selectMenuByUserId(userId);
        if (perList == null || perList.isEmpty()) {
            return null;
        }
        // 申明菜单树
        List<MenuVO> menuList = new ArrayList<MenuVO>();
        //得到导航栏菜单树
        for (MenuVO mv:perList){
            if (mv.getPid()==0){
                menuList.add(mv);
            }
        }
        //得到菜单树下的所有菜单
        for (MenuVO mv : menuList) {
            getChild(mv,perList);
        }
        return menuList;
    }

    private void getChild(MenuVO mv,List<MenuVO> perList) {
        // 申明子菜单
        List<MenuVO> childList = new ArrayList<MenuVO>();
        // 遍历所有菜单，将父菜单id与传过来的Pid比较
        for (MenuVO menu : perList) {
            if (menu.getPid().equals(mv.getId())){
                childList.add(menu);
            }
        }
        if (childList.size()==0){
            return;
        }
        for (MenuVO menu:childList){
            getChild(menu,perList);
        }
        mv.setMvList(childList);
    }
    @Override
    public boolean isPermitted(Token token, String permission) {
        /**
         *
         * 菜单级别、权限验证，生产环境建议加上缓存处理。
         *
         */
        if (StringUtils.isNotBlank(permission)) {
            IPermissionService service =  SpringBeanUtil.getBean(IPermissionService.class);
            List<Permission> pl = service.selectAllByUserId(token.getId());
            if (pl != null) {
                for (Permission p : pl) {
                    if (permission.equals(p.getPermCode())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Cacheable(value = "permissionCache", key = "#userId")
    @Override
    public List<Permission> selectAllByUserId(Long userId) {
        return baseMapper.selectAllByUserId(userId);
    }

    @Override
    public boolean isActionable(Token token, String permission) {
        /**
         *
         * 按钮级别、权限验证，生产环境建议加上缓存处理。
         * <br>
         * 演示  admin 返回 true
         *
         */
        System.err.println(" isActionable =" + permission);
        if (token.getId() == 1L) {
            return true;
        }
        return false;
    }

}